import { _decorator, Component, EventTouch, Node, random, UITransform, Vec2, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Joystick')
export class Joystick extends Component {
    @property(Node)
    private handle: Node = null;
    @property(Node)
    private panel: Node = null;
    /**摇杆的半径 */
    private _radius: number = 0;

    private _touchLocation: Vec2 = new Vec2();
    private _touchWorldPos: Vec3 = new Vec3();
    private _curPos: Vec3 = new Vec3();
    private _startPos: Vec3 = new Vec3(0, 0);

    private _arrTouchEvent: any[] = [0, 1];

    private _listener: Function;
    private _target: any;

    static readonly Event = {
        START: 0,
        MOVE: 1,
        END: 2,
        CANCEL: 3
    }

    protected onEnable(): void {
        this._radius = this.panel.getComponent(UITransform).contentSize.width / 4;

        this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
    }

    protected onDisable(): void {
        this.node.off(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.off(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.off(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
    }

    start() {

    }

    update(deltaTime: number) {

    }
    onTouchStart(event: EventTouch) {
        this.notify(Joystick.Event.START);

        event.getUIStartLocation(this._touchLocation);
        this.node.getComponent(UITransform).convertToNodeSpaceAR(this._touchWorldPos.set(this._touchLocation.x, this._touchLocation.y), this._startPos);
        this.panel.setPosition(this._startPos);
        this.handle.setPosition(this._startPos);
    }

    onTouchMove(event: EventTouch) {
        event.getUILocation(this._touchLocation);
        this.node.getComponent(UITransform).convertToNodeSpaceAR(this._touchWorldPos.set(this._touchLocation.x, this._touchLocation.y), this._curPos);

        const distance = Vec2.distance(this._startPos, this._curPos);

        const radian = Math.atan2(this._curPos.y - this._startPos.y, this._curPos.x - this._startPos.x);

        if (distance < this._radius) {
            this.handle.setPosition(this._curPos);
        } else {
            const x = this._startPos.x + Math.cos(radian) * this._radius;
            const y = this._startPos.y + Math.sin(radian) * this._radius;
            this.handle.setPosition(x, y);
        }

        this.notify(Joystick.Event.MOVE, radian);
    }

    onTouchEnd() {
        this.handle.setPosition(this._startPos);
        this.notify(Joystick.Event.END);
    }

    onTouchCancel() {
        this.handle.setPosition(this._startPos);
        this.notify(Joystick.Event.CANCEL);
    }



    onTouchEvent(listener: Function, target?: any) {
        this._listener = listener;
        this._target = target;
    }

    private notify(event: number, radian?: number) {
        this._arrTouchEvent[0] = event;
        this._arrTouchEvent[1] = radian;
        this._listener.apply(this._target, this._arrTouchEvent);
    }



}


